package com.guoxue.action.front;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.guoxue.domain.CollectAndTransferVo;
import com.guoxue.domain.UserVo;
import com.guoxue.service.ICollectAndTransferService;
import com.guoxue.service.ITapesRecordsService;
import com.guoxue.service.IUserService;
import com.guoxue.util.LoggerUtil;
import com.guoxue.util.OtherUtils;

/**
 * 接收并返回录音的收藏和转发
 */
@Controller
@RequestMapping("/User")
public class CollectAndTransferAction extends BaseAction{
	
	@Resource
	private IUserService userService;
	@Resource
	private ICollectAndTransferService collectAndTransferService;
	@Resource
	private ITapesRecordsService tapesRecordsVoService;
	
	@ResponseBody
	@RequestMapping("/collectAndTransferCommit")
	public JSON collectAndTransferCommit(HttpServletRequest req, HttpServletResponse resp,Integer type,String flag){
		try {
			if (flag == null && "".equals(flag)){
				return Message(ERROR, "未指定作品id", null);
			}
			if (type == null && "".equals(type)){
				return Message(ERROR, "未指定类型", null);
			}else{
				if(!type.equals(1) && !type.equals(2)){
					return Message(ERROR, "指定类型错误", null);
				}
			}
			UserVo user = (UserVo) req.getSession().getAttribute("user");
			if (user.getId()==0){
				return Message(ERROR, "获取Session用户信息失败", null);
			}
			Long tapesToatl = tapesRecordsVoService.getTapesRecordsTotal(user.getId(),flag);
			if(tapesToatl <= 0){
				return Message(ERROR, "请先上传再进行转发或者收藏", null);
			}
			//(1.转发2.收藏)
			Long count = collectAndTransferService.getCollectAndTransferTotal(user.getId(),type);
			if(count >= 100 || "100".equals(count)){
				//转发、收藏为何要设置上限呢？
				if(type == 1 || "1".equals(type)){
					return Message(ERROR, "您的转发已经达到上限", null);
				}else{
					return Message(ERROR, "您的收藏已经达到上限", null);
				}
			}
			CollectAndTransferVo collectAndTransfer = new CollectAndTransferVo();
			collectAndTransfer.setCommitTime(OtherUtils.getCurrentDate());
			collectAndTransfer.setFlag(flag);
			collectAndTransfer.setSysflag(0);
			collectAndTransfer.setType(type);
			collectAndTransfer.setUid(user.getId());
			collectAndTransferService.saveCollectAndTransfer(collectAndTransfer);
			return Message(SUCCESS, "上传成功", null);
		} catch (Exception e) {
			return LoggerUtil.logInfo(e, "上传异常");
		}
	}
	
	@ResponseBody
	@RequestMapping("/collectAndTransferList")
	public JSON collectAndTransferList(HttpServletRequest req, HttpServletResponse resp,Integer type){
		try {
			if (type == null){
				return Message(ERROR, "未指定类型", null);
			}else{
				if(!type.equals(1) && !type.equals(2)){
					return Message(ERROR, "指定类型错误", null);
				}
			}
			UserVo user = (UserVo) req.getSession().getAttribute("user");
			if (user.getId()==0){
				return Message(ERROR, "获取Session用户信息失败", null);
			}
			List<CollectAndTransferVo> CollectAndTransferVoList = collectAndTransferService.getCollectAndTransferList();
			if(CollectAndTransferVoList.size() > 0){
				return Message (SUCCESS, "获取数据成功", CollectAndTransferVoList);
			}else{
				return Message(SUCCESS, "获取数据成功", null);
			}
			
		} catch (Exception e) {
			return LoggerUtil.logInfo(e, "获取数据失败");
		}
	}
	
	@ResponseBody
	@RequestMapping("/deleteCollectAndTransfer")
	public JSON deleteCollectAndTransfer(HttpServletRequest req, HttpServletResponse resp,Integer id){
		try {
			UserVo user = (UserVo) req.getSession().getAttribute("user");
			if (user.getId()==0){
				return Message(ERROR, "获取Session用户信息失败", null);
			}
			if(null == id){
				return Message(ERROR, "参数错误", null);
			}
			CollectAndTransferVo collectAndTransfer = collectAndTransferService.getCollectAndTransfer(id);
			if(collectAndTransfer != null){
				collectAndTransferService.deleteCollectAndTransfer(id);
				return Message(SUCCESS, "删除数据成功", null);
			}else{
				return Message(ERROR, "删除数据失败", null);
			}
		} catch (Exception e) {
			return LoggerUtil.logInfo(e, "删除数据失败");
		}
	}
	
}
